package com.hollycrm.unicom.sheet.code;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hollycrm.springframework.cache.CacheDepthUtil;
import com.hollycrm.springframework.cache.CacheException;
import com.hollycrm.unicom.sheet.define.ClassTreeViewObject;
import com.hollycrm.unicom.code.GeneralCode;
import com.hollycrm.unicom.sheet.define.SheetCategory;
import com.hollycrm.unicom.usersupport.DepartmentTreeViewObject;

/**
 * CacheCodeManagerProxyBean
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
public class CacheCodeManagerProxyBean implements CodeManager {

    private final static String DEFAULT_REGION = "default";

    private Log logger = LogFactory.getLog(getClass());

    private CodeManager target;

    private String deptTreeListKey = "deptTreeList";
    private String categoryListKey = "categoryList";

    private String cacheRegionName1;
    private String cacheRegionName2;
    private String cacheRegionName3;
    private String cacheRegionName4;
    private String cacheRegionName5;
    private String cacheRegionName6;
    private String cacheRegionName7;

    public CacheCodeManagerProxyBean() {
        cacheRegionName1 = DEFAULT_REGION;
        cacheRegionName2 = DEFAULT_REGION;
        cacheRegionName3 = DEFAULT_REGION;
        cacheRegionName4 = DEFAULT_REGION;
        cacheRegionName5 = DEFAULT_REGION;
        cacheRegionName6 = DEFAULT_REGION;
        cacheRegionName7 = DEFAULT_REGION;
    }

    /**
     * getDepartmentTreeList
     *
     * @return List
     */
    public List getDepartmentTreeList() {
        try {
            List treeList = CacheDepthUtil.getList(cacheRegionName1, deptTreeListKey);
            if (treeList == null || treeList.isEmpty()) {
                treeList = target.getDepartmentTreeList();
                if (!treeList.isEmpty())
                    CacheDepthUtil.putList(cacheRegionName1, deptTreeListKey, treeList, "id");
            }
            return treeList;
        }
        catch (CacheException ex) {
            logger.warn("[w] " + ex.getMessage());
            return Collections.EMPTY_LIST;
        }
    }

    /**
     * getDepartment
     *
     * @param deptID String
     * @return DepartmentTreeViewObject
     */
    public DepartmentTreeViewObject getDepartment(String deptID) {
        try {
            DepartmentTreeViewObject object = (DepartmentTreeViewObject) CacheDepthUtil.get(
                cacheRegionName1, deptTreeListKey, DepartmentTreeViewObject.class, deptID);
            if (object == null) {
                object = target.getDepartment(deptID);
                if (object != null)
                    CacheDepthUtil.put(cacheRegionName1,
                                       generateKey(deptTreeListKey, DepartmentTreeViewObject.class,
                                                   deptID), object);
                else object = new DepartmentTreeViewObject(null, null, null, null, null, 0);
            }
            return object;
        }
        catch (CacheException ex) {
            logger.warn("[w] " + ex.getMessage());
            return new DepartmentTreeViewObject(null, null, null, null, null, 0);
        }
    }

    /**
     * getGeneralCode
     *
     * @param codeType String
     * @param codeID String
     * @return GeneralCode
     */
    public GeneralCode getGeneralCode(String codeType, String codeID) {
        try {
            GeneralCode object = (GeneralCode) CacheDepthUtil.get(
                cacheRegionName2, codeType, GeneralCode.class, codeID);
            if (object == null) {
                object = target.getGeneralCode(codeType, codeID);
                if (object != null)
                    CacheDepthUtil.put(cacheRegionName2,
                                       generateKey(codeType, GeneralCode.class,
                                                   codeID), object);
                else object = new GeneralCode();
            }
            return object;
        }
        catch (CacheException ex) {
            logger.warn("[w] " + ex.getMessage());
            return new GeneralCode();
        }
    }

    /**
     * getGeneralCodeList
     *
     * @param codeType String
     * @return List
     */
    public List getGeneralCodeList(String codeType) {
        try {
            List list = CacheDepthUtil.getList(cacheRegionName2, codeType);
            if (list == null || list.isEmpty()) {
                list = target.getGeneralCodeList(codeType);
                if (!list.isEmpty())
                    CacheDepthUtil.putList(cacheRegionName2, codeType, list, "codeID");
            }
            return list;
        }
        catch (CacheException ex) {
            logger.warn("[w] " + ex.getMessage());
            return Collections.EMPTY_LIST;
        }
    }

    /**
     * getProblemClass
     *
     * @param problemID String
     * @return ClassTreeViewObject
     */
    public ClassTreeViewObject getProblemClass(String problemID) {
        try {
            ClassTreeViewObject object = (ClassTreeViewObject) CacheDepthUtil.get(
                cacheRegionName3, "", ClassTreeViewObject.class, problemID);
            if (object == null) {
                object = target.getProblemClass(problemID);
                if (object != null)
                    CacheDepthUtil.put(cacheRegionName3,
                                       generateKey("", ClassTreeViewObject.class, problemID), object);
                else object = new ClassTreeViewObject(null, null, null, null, 0);
            }
            return object;
        }
        catch (CacheException ex) {
            logger.warn("[w] " + ex.getMessage());
            return new ClassTreeViewObject(null, null, null, null, 0);
        }
    }

    /**
     * getSheetCategoryList
     *
     * @return List
     */
    public List getSheetCategoryList() {
        try {
            List list = CacheDepthUtil.getList(cacheRegionName4, categoryListKey);
            if (list == null || list.isEmpty()) {
                list = target.getSheetCategoryList();
                if (!list.isEmpty())
                    CacheDepthUtil.putList(cacheRegionName4, categoryListKey, list,
                                           "categoryID");
            }
            return list;
        }
        catch (CacheException ex) {
            logger.warn("[w] " + ex.getMessage());
            return Collections.EMPTY_LIST;
        }
    }

    /**
     * getSheetCategory
     *
     * @param categoryID String
     * @return SheetCategory
     */
    public SheetCategory getSheetCategory(String categoryID) {
        try {
            SheetCategory object = (SheetCategory) CacheDepthUtil.get(
                cacheRegionName4, categoryListKey, SheetCategory.class, categoryID);
            if (object == null) {
                object = target.getSheetCategory(categoryID);
                if (object != null)
                    CacheDepthUtil.put(cacheRegionName4,
                                       generateKey(categoryListKey, SheetCategory.class,
                                                   categoryID), object);
                else object = new SheetCategory();
            }
            return object;
        }
        catch (CacheException ex) {
            logger.warn("[w] " + ex.getMessage());
            return new SheetCategory();
        }
    }

    /**
     * getProblemClassTreeList
     *
     * @param treeType String
     * @return List
     */
    public List getProblemClassTreeList(String treeType) {
        try {
            List treeList = CacheDepthUtil.getList(cacheRegionName3, treeType);
            if (treeList == null || treeList.isEmpty()) {
                treeList = target.getProblemClassTreeList(treeType);
                if (!treeList.isEmpty())
                    CacheDepthUtil.putList(cacheRegionName3, treeType, treeList, "id");
            }
            return treeList;
        }
        catch (CacheException ex) {
            logger.warn("[w] " + ex.getMessage());
            return Collections.EMPTY_LIST;
        }
    }

    /**
     * getProblemTemplate
     *
     * @param problemID String
     * @return List
     */
    public List getProblemTemplate(String problemID) {
        try {
            List list = CacheDepthUtil.getList(cacheRegionName5, problemID);
            if (list == null || list.isEmpty()) {
                list = target.getProblemTemplate(problemID);
                if (!list.isEmpty())
                    CacheDepthUtil.putList(cacheRegionName5, problemID, list, "fieldCode");
            }
            return list;
        }
        catch (CacheException ex) {
            logger.warn("[w] " + ex.getMessage());
            return Collections.EMPTY_LIST;
        }
    }

    /**
     * getProblemOtherRelation
     *
     * @param problemID String
     * @return List
     */
    public List getProblemOtherRelation(String problemID) {
        try {
            List list = CacheDepthUtil.getList(cacheRegionName6, problemID);
            if (list == null || list.isEmpty()) {
                list = target.getProblemOtherRelation(problemID);
                if (!list.isEmpty())
                    CacheDepthUtil.putList(cacheRegionName6, problemID, list, "rowID");
            }
            return list;
        }
        catch (CacheException ex) {
            logger.warn("[w] " + ex.getMessage());
            return Collections.EMPTY_LIST;
        }

    }

    /**
     * getProblemFlowID
     *
     * @param problemID String
     * @return String
     */
    public String getProblemFlowID(String problemID) {
        try {
            String object = (String) CacheDepthUtil.get(
                cacheRegionName7, problemID);
            if (object == null) {
                object = target.getProblemFlowID(problemID);
                if (object != null)
                    CacheDepthUtil.put(cacheRegionName7, problemID, object);
            }
            return object;
        }
        catch (CacheException ex) {
            logger.warn("[w] " + ex.getMessage());
            return null;
        }

    }

    private static Object generateKey(Object listKey, Class clazz, String fieldKey) {
        Object key = listKey + "." + clazz.getName() + fieldKey;
        return key;
    }

    public CodeManager getTarget() {
        return target;
    }
    public void setTarget(CodeManager target) {
        this.target = target;
    }
    public String getCacheRegionName1() {
        return cacheRegionName1;
    }
    public void setCacheRegionName1(String cacheRegionName1) {
        this.cacheRegionName1 = cacheRegionName1;
    }
    public String getCacheRegionName2() {
        return cacheRegionName2;
    }
    public void setCacheRegionName2(String cacheRegionName2) {
        this.cacheRegionName2 = cacheRegionName2;
    }
    public String getCacheRegionName3() {
        return cacheRegionName3;
    }
    public void setCacheRegionName3(String cacheRegionName3) {
        this.cacheRegionName3 = cacheRegionName3;
    }
    public String getDeptTreeListKey() {
        return deptTreeListKey;
    }
    public void setDeptTreeListKey(String deptTreeListKey) {
        this.deptTreeListKey = deptTreeListKey;
    }
    public String getCategoryListKey() {
        return categoryListKey;
    }
    public String getCacheRegionName4() {
        return cacheRegionName4;
    }
    public void setCategoryListKey(String categoryListKey) {
        this.categoryListKey = categoryListKey;
    }
    public void setCacheRegionName4(String cacheRegionName4) {
        this.cacheRegionName4 = cacheRegionName4;
    }
    public String getCacheRegionName5() {
        return cacheRegionName5;
    }
    public void setCacheRegionName5(String cacheRegionName5) {
        this.cacheRegionName5 = cacheRegionName5;
    }
    public String getCacheRegionName6() {
        return cacheRegionName6;
    }
    public void setCacheRegionName6(String cacheRegionName6) {
        this.cacheRegionName6 = cacheRegionName6;
    }
    public String getCacheRegionName7() {
        return cacheRegionName7;
    }
    public void setCacheRegionName7(String cacheRegionName7) {
        this.cacheRegionName7 = cacheRegionName7;
    }

    public List getProblemClassTreeListBySpell(String spellStr) {
      return target.getProblemClassTreeListBySpell(spellStr);
    }

    public double getCustomerLevelPriority(final String customerLevel, final boolean isUsed){
      return target.getCustomerLevelPriority(customerLevel, isUsed);
    }

    /**
         * getDepartmentTreeList
         *
         * @return List
         */
        public List getDepartmentTreeList(String deptID) {
            try {
                List treeList = new ArrayList();
                if (treeList == null || treeList.isEmpty()) {
                    treeList = target.getDepartmentTreeList();
                    if (!treeList.isEmpty())
                        CacheDepthUtil.putList(cacheRegionName1, deptTreeListKey, treeList, "id");
                }
                return treeList;
            }
            catch (CacheException ex) {
                logger.warn("[w] " + ex.getMessage());
                return Collections.EMPTY_LIST;
            }
    }

}
